package cn.fyl.product.service.impl;

import cn.fyl.basic.exception.BusinessException;
import cn.fyl.basic.service.IBaiduAiAuditService;
import cn.fyl.basic.service.impl.BaiduAiAuditServiceImpl;
import cn.fyl.product.domain.Product;
import cn.fyl.product.domain.ProductDetail;
import cn.fyl.product.mapper.ProductDetailMapper;
import cn.fyl.product.mapper.ProductMapper;
import cn.fyl.product.service.IProductService;
import cn.fyl.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;

    @Override
    public void remove(Long id) {
        productMapper.remove(id);
        productDetailMapper.remove(id);
    }

    @Override
    public void save(Product product) {

        Boolean textCensor = baiduAiAuditService.textAudit(product.getName());
        Boolean imageCensor = baiduAiAuditService.imageAudit(product.getResources());
        if(!textCensor && !imageCensor){
            throw new BusinessException("参数非法!");
        }
        productMapper.save(product);
        ProductDetail detail = product.getProductDetail();
        if (detail!=null){
            detail.setProductId(product.getId());
            productDetailMapper.save(detail);
        }


    }

    @Override
    public void update(Product product) {
        productMapper.update(product);
        //是否有详情
        ProductDetail detail = product.getProductDetail();
        if (detail!=null){
            if (detail.getId()!=null){
                //有详情
                productDetailMapper.update(detail);
            }else{
                productDetailMapper.save(detail);
            }
        }
    }

    @Override
    public void onSale(List<Long> ids) {
        Date date = new Date();
        productMapper.onSale(ids,date);

    }

    @Override
    public void offSale(List<Long> ids) {

        Date date = new Date();
        productMapper.offSale(ids,date);

    }
}
